#include "charactercountercreator.h"
#include "ui_charactercountercreator.h"
#include "../database.h"

CharacterCounterCreator::CharacterCounterCreator(NameGiver *ng, QWidget *parent) :
  QDialog(parent),
  ui(new Ui::CharacterCounterCreator)
{
  ui->setupUi(this);
  if (ng != nullptr)
  {
    ui->le_name->setText(ng->name());
    ui->le_dp->setText(QString::number(ng->characteristic(ed::Char_PhysicalDefense)));
    ui->le_dm->setText(QString::number(ng->characteristic(ed::Char_MagicDefense)));
    ui->le_ds->setText(QString::number(ng->characteristic(ed::Char_SocialDefense)));
    ui->le_death->setText(QString::number(ng->characteristic(ed::Char_Death)));
    ui->le_uncoun->setText(QString::number(ng->characteristic(ed::Char_Uncounciousness)));
    ui->le_injury->setText(QString::number(ng->characteristic(ed::Char_Injury)));
    ui->le_ap->setText(QString::number(ng->characteristic(ed::Char_PhysicalArmor)));
    ui->le_am->setText(QString::number(ng->characteristic(ed::Char_MysticalArmor)));
    ui->le_move->setText(QString::number(ng->characteristic(ed::Char_WalkSpeed)));
    ui->le_run->setText(QString::number(ng->characteristic(ed::Char_RunSpeed)));
  }
}

CharacterCounterCreator::~CharacterCounterCreator()
{
  delete ui;
}

int dpmm(double mm)
{
  return mm * 11.811023622;
}

#include <QPainter>
#include <QImage>
#include <QFontDatabase>

int cell=0;
int c_uncouncious = 0;
int c_death = 0;
void drawDamageCell(QPainter* p, double x, double y)
{
  ++cell;

  if (cell > c_death) return;

  QColor penColor = (cell <= c_death - c_uncouncious ? QColor(77,15,17) : QColor(54,54,54));
  QPen pen(penColor);
  pen.setWidth(dpmm(0.355));
  p->setPen(pen);

  QRect r(dpmm(x),dpmm(y),dpmm(8),dpmm(8));
  p->drawRect(r);

  p->setPen(QPen(penColor));
  p->drawText(r,Qt::AlignCenter | Qt::AlignVCenter,QString::number(cell));
}

void CharacterCounterCreator::save(const QString &path)
{
  if (path.isNull()) return;
  QString outPath = path;
  if (!outPath.endsWith(".png")) outPath += ".png";

  QFontDatabase fontDB;
  QImage img("://images/base_compteur.png");
  QPainter p(&img);
  QFont f;
  f.setPointSize(7);
  f.setItalic(true);
  p.setFont(f);

  // Blessure grave
  QString injuryText = ui->le_injury->text();
  p.drawText(dpmm(52.8),dpmm(45.076),dpmm(38.7),dpmm(2.6),Qt::AlignVCenter | Qt::AlignLeft,injuryText);

  // Saignement
  QString bleedText = QString::number(Database::requiredResult(ui->le_injury->text().toInt(),ed::Excellent));
  p.drawText(dpmm(52.8),dpmm(50.126),dpmm(38.7),dpmm(2.6),Qt::AlignVCenter | Qt::AlignLeft,bleedText);

  // Move
  QString moveText = ui->le_move->text() + " / " + ui->le_run->text();
  p.drawText(dpmm(52.8),dpmm(55.065),dpmm(38.7),dpmm(2.6),Qt::AlignVCenter | Qt::AlignLeft,moveText);

  // DP
  QString dpText = ui->le_dp->text() + " (" + QString::number(Database::requiredResult(ui->le_dp->text().toInt(),ed::Excellent)) + ")";
  p.drawText(dpmm(117.25),dpmm(45.076),dpmm(38.7),dpmm(2.6),Qt::AlignVCenter | Qt::AlignLeft,dpText);

  // DM
  QString dmText = ui->le_dm->text() + " (" + QString::number(Database::requiredResult(ui->le_dm->text().toInt(),ed::Excellent)) + ")";
  p.drawText(dpmm(117.25),dpmm(50.126),dpmm(38.7),dpmm(2.6),Qt::AlignVCenter | Qt::AlignLeft,dmText);

  // DS
  QString dsText = ui->le_ds->text() + " (" + QString::number(Database::requiredResult(ui->le_ds->text().toInt(),ed::Excellent)) + ")";
  p.drawText(dpmm(117.25),dpmm(55.065),dpmm(38.7),dpmm(2.6),Qt::AlignVCenter | Qt::AlignLeft,dsText);

  // Name
  QFont f2 = fontDB.font("Willow", "Regular", 24);
  p.setFont(f2);
  p.drawText(dpmm(36.51),dpmm(29.65),dpmm(134.49),dpmm(10.2),Qt::AlignVCenter | Qt::AlignCenter,ui->le_name->text());

  // Squares
  cell=0;
  c_uncouncious = ui->le_uncoun->text().toInt();
  c_death = ui->le_death->text().toInt();

  p.setBrush(Qt::transparent);
  p.setFont(f);

  double margin = 2.150;
  int w = 8;
  int h = 8;
  int squarePerRow = 17;
  int squarePerCol = 24;
  double startX = 4.0125;
  double startY = 1.375;


  double x = 11.775 + startX;
  double y = 9.65 + startY;
  for (int i=0;i<squarePerRow;++i)
  {
    drawDamageCell(&p,x,y);
    x+=w+margin;
  }
  for (int i=0;i<squarePerCol;++i)
  {
    drawDamageCell(&p,x,y);
    y+=h+margin;
  }
  for (int i=0;i<squarePerRow;++i)
  {
    drawDamageCell(&p,x,y);
    x-=w+margin;
  }
  for (int i=0;i<squarePerCol;++i)
  {
    drawDamageCell(&p,x,y);
    y-=h+margin;
  }
  squarePerCol-=2;
  squarePerRow-=2;
  x+=w+margin*0.75;
  y+=h+margin*0.75;

  for (int i=0;i<squarePerRow;++i)
  {
    drawDamageCell(&p,x,y);
    x+=w+margin;
  }
  for (int i=0;i<squarePerCol;++i)
  {
    drawDamageCell(&p,x,y);
    y+=h+margin;
  }
  for (int i=0;i<squarePerRow;++i)
  {
    drawDamageCell(&p,x,y);
    x-=w+margin;
  }
  for (int i=0;i<squarePerCol;++i)
  {
    drawDamageCell(&p,x,y);
    y-=h+margin;
  }

  img.save(outPath);
}
